﻿using System.Windows.Media;
using Shatkotha.WPF.Global.Bases;
using Shatkotha.WPF.Global.Modules.Contacts;
using System.Windows.Controls;

namespace Shatkotha.WPF.Contacts.Views
{
    /// <summary>
    /// Interaction logic for ContactEditorView.xaml
    /// </summary>
    public partial class ContactEditorView : IContactEditorView
    {
        private IContactEditorPresenter _presenter;

        public ContactEditorView()
        {
            InitializeComponent();
            SetDefaultTextColor();
        }

        public IPresenter Presenter
        {
            set { _presenter = value as IContactEditorPresenter;
                DataContext = value; }
        }

        private bool IsValidTextBox(TextBox textBox)
        {
            if (string.IsNullOrEmpty(textBox.Text))
            {
                textBox.BorderBrush = new SolidColorBrush(Colors.Red);
                return false;
            }
            txtName.BorderBrush = new SolidColorBrush(Colors.DarkGray);
            return true;
        }

        public bool IsValid()
        {
            if (IsValidTextBox(txtName) &&
            IsValidTextBox(txtAge) &&
            IsValidTextBox(txtLocation) &&
            IsValidTextBox(txtEmail) &&
            IsValidTextBox(txtPhone))
                return true;   
            return false;
        }

        private void SetDefaultTextColor()
        {
            txtName.BorderBrush = new SolidColorBrush(Colors.DarkGray);
            txtAge.BorderBrush = new SolidColorBrush(Colors.DarkGray);
            txtSex.BorderBrush = new SolidColorBrush(Colors.DarkGray);
            txtLocation.BorderBrush = new SolidColorBrush(Colors.DarkGray);
            txtEmail.BorderBrush = new SolidColorBrush(Colors.DarkGray);
            txtPhone.BorderBrush = new SolidColorBrush(Colors.DarkGray);
        }

        private void txtName_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            IsValidTextBox(txtName);
        }

        private void txtAge_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            IsValidTextBox(txtAge);
        }

        private void txtLocation_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            IsValidTextBox(txtLocation);
        }

        private void txtEmail_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            IsValidTextBox(txtEmail);
        }

        private void txtPhone_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            IsValidTextBox(txtPhone);
        }
    }
}
